/*
Realistic Unit Modification v4.0 Dialog 
v4.0.0.1 [ALPHA]
Concept & Development by Draper & Raps | [VRC]
*/

private ["_helo","_airport","_egress","_ingress","_home","_callsign","_pilot","_gunner","_grp","_mkrname","_mkrname2","_wpAH","_lz","_heloRTBWaypoint","_vehchk","_distanceToIngress","_heloRTBWaypointH","_distanceToDeck","_heightToHomePlate","_distToHomePlate"];

/* create variabled */
_helo = _this select 0;
_airport = _this select 3;
_egress = _this select 4;
_ingress = _this select 5;
_home = _this select 2;
_callsign = _this select 1;

/* define pilot, gunner, and group */
_pilot = driver _helo;
_gunner = gunner _helo;
_grp = group _pilot;

/* removes the markers created */
_mkrname = format ["%1station",_helo];
deletemarkerLocal _mkrname;
_mkrname2 = format ["%1station2",_helo];
deletemarkerLocal _mkrname2;

/* dialog from pilot to infantry */
["RTB",[player,_pilot,_callsign]] execVM (RUM_PATH+"AHel\radiodialogs.sqf");
sleep 8;

/* spawn a loop to monitor the helo if its still alive or has been destroyed */
[_helo] spawn {
	_helo = _this select 0;
	_vehchk = true;
	while {_vehchk} do {
		If (!alive _helo) then {
			player sidechat format ["%1 has crashed or has been destroyed.", _callsign];
			hintsilent format ["%1 has crashed or has been destroyed.", _callsign];
			_vehchk = false;
		};
		sleep 1;
	};
};
	
/* waypoints to move helo to ingress pos then to landing */
_wpAH =_grp addWaypoint [getpos _ingress, 0];
_wpAH setWaypointType "MOVE";
_wpAH setWaypointSpeed "LIMITED";
_wpAH setWaypointBehaviour "CARELESS";
_wpAH setWaypointCombatMode "BLUE";
_wpAH setWaypointCompletionRadius 50;
_helo flyInheight 32;
sleep 4.75;

_wpAH=_grp addWaypoint [getpos _ingress, 0];
_wpAH setWaypointType "MOVE";
_wpAH setWaypointSpeed "NORMAL";
_wpAH setWaypointBehaviour "CARELESS";
_wpAH setWaypointCombatMode "BLUE";
_wpAH setWaypointCompletionRadius 50;
_grp setcurrentWaypoint _wpAH;
_helo flyInheight RUM_AHEL_flyHeight;

sleep 1;

/* checks for final approach and whether the landing is on a LHD or ground */
if (surfaceIsWater (getpos _home)) then {
	_heloRTBWaypoint = true;
	
	while {_heloRTBWaypoint} do {
		sleep .05;
		_distanceToIngress = round(_ingress distance _helo);
		hintsilent format["distance from home plate: %1",_distanceToIngress];
		if (_distanceToIngress <= 200) then {
			_wpAH = _grp addWaypoint [getposASL _home, 0];
			_wpAH setWaypointType "GETOUT";
			_wpAH setWaypointSpeed "LIMITED";
			_wpAH setWaypointBehaviour "CARELESS";
			_wpAH setWaypointCombatMode "BLUE";
			_wpAH setWaypointCompletionRadius 0;

			/* ends the while loop. */
			_heloRTBWaypoint = false;
		};
	};
	
	/* starts new while loop for landing procedures... */
	_heloRTBWaypointH = true;
	
	while {_heloRTBWaypointH} do {
		sleep 0.05;
		_distanceToDeck = round(getposASL _helo select 2);
		_heightToHomePlate = round(((getposASL _helo select 2)) - ((getposASL _home select 2)* 0 + RUM_AHEL_CarrierDeck));
		_distToHomePlate = round( _helo distance [(getposASL _home select 0),(getposASL _home select 1),((getposASL _home select 2)* 0 + RUM_AHEL_CarrierDeck)]);
		if (_distToHomePlate <= 70) then {
			if (_heightToHomePlate <= 5) then {
			sleep 3.5;
			_helo setFuel 0;
			sleep 5;

			/* ends the while loop. */
			_heloRTBWaypointH = false;
			}else{
				/* debug */
				hintsilent format ["Height Above Home Plate: %1\nDistance to Home Plate: %2",_heightToHomePlate,_distToHomePlate];
			};
		}else{
			/* debug */
			hintsilent format ["Height Above Home Plate: %1",_heightToHomePlate];
		};
	};
} else {
	/* This section of the if statment is for land based helo's */
	
	/* waits till closer to ingress position before moving to final approach. */
	waituntil{
		sleep 0.15;
		((_ingress distance [getPos _helo select 0, getPos _helo select 1, 0]) <= 200);
	};

	_wpAH=_grp addWaypoint [getpos _home, 0];
	_wpAH setWaypointType "GETOUT";
	_wpAH setWaypointSpeed "NORMAL";
	_wpAH setWaypointBehaviour "CARELESS";
	_wpAH setWaypointCombatMode "BLUE";
	_wpAH setWaypointCompletionRadius 50;
	_helo flyInheight (RUM_AHEL_flyHeight - 45);
	_stg = "RUM_AHel_endflight = '"+(format ["%1",_helo])+"'";
	_wpAH setWaypointStatements ["true", _stg];
	
	/* end of flight check when helo has landed */
	_end = format ["%1",_helo];
	sleep 5;
	RUM_AHel_endflight = "";
	waitUntil{
		sleep 0.15;
		(!alive _helo) or (RUM_AHel_endflight == _end);
	};
};

/* turn helos engines off, remove pilot and gunner */
// hint "deleting pilots and stuff";
_pilot action ["engineOff", _helo];
unassignVehicle _pilot;
doGetOut [_pilot,_gunner];
unassignVehicle _gunner;
doGetOut [_pilot,_gunner];
deleteVehicle _pilot;
deleteVehicle _gunner;

/* removed any invisible helo pads that were created*/
if (!isNil "_lz") then {
	deleteVehicle _lz;
};

/* waits till crew are deleted */
waitUntil{
	sleep 0.5;
	(count(crew _helo) == 0);
};

/* set the helo back in the correct position and direction */
if (alive _helo) then {
	player sidechat format ["%1 has landed safely.", _callsign];
	player sidechat format ["%1 is being rearmed/refueled at this time and will be avaiable in %2 mics.", _callsign, round(RUM_AHEL_ReSupTime/60)];
	_helo setdir getdir _home;

	/* repairs, rearms and refuels the helo */ 
	_helo setDamage 0;
	_helo setFuel 1;
	_mags = magazinesAmmo _helo;
	// _mags = getArray (configFile>>"cfgVehicles">>typeOf _helo>>"Turrets">>"MainTurret">>"magazines");
	{_helo removeMagazine _x} forEach magazines _helo;
	{_helo addMagazine _x} forEach _mags;
};

/* timer for when the helo is avaialble for use again */
sleep RUM_AHEL_ReSupTime;

/* dialog to player from pilot */
["afterRTB",[player,_pilot,_callsign]] execVM (RUM_PATH+"AHel\radiodialogs.sqf");
// _pilot sidechat format["%1 has been serviced and is ready for deployment at request.",_callsign];

/* creates the helo entry in the PDA when wait timer is reached */
RUM_AHelAircraft = RUM_AHelAircraft + [_this];
publicVariable "RUM_AHelAircraft";

/* sets the flight status of the helo */
_helo setVariable ["RUM_inFlight", false, true];

/* ends the script */
if (true) exitwith {};